'use strict';

const Controller = require('egg').Controller;
let carList = []

class HomeController extends Controller {
  /********************* react+ts购物车请求  getCarList 、getCarDetail 、getCar 、addCarList 、getCar 、addCar 、delCar**********/

  // 获取购物车数据
  async getCarList() {
    const { ctx } = this
    const list = await this.app.mysql.select('carlist')
    if (list) {
      ctx.body = {
        code: 200,
        list
      }
    }
  }

  // 详情
  async getCarDetail() {
    const { ctx } = this
    const { id } = ctx.request.body
    const list = await this.app.mysql.get('carlist', { id })
    ctx.body = {
      code: 200,
      list
    }
  }

  // 加入购物车
  async addCarList() {
    const { ctx } = this
    const { id, title, price, img } = ctx.request.body

    // 查找id
    const post = await this.app.mysql.get('car', { infoId: id });
    // 如果找不到id就插入
    if (post === null) {
      const result = await this.app.mysql.insert('car', {
        title,
        price,
        img,
        num: 1,
        infoId: id
      })
      // 如果插入成功就重新获取全表
      if (result) {
        const carList = await this.app.mysql.select('car')
        ctx.body = {
          code: 200,
          carList
        }
      }
    }
    //以infoId = id为条件更新数据的num
    else {
      const row = {
        num: post.num + 1
      };

      const options = {
        where: {
          infoId: id
        }
      };
      const result = await this.app.mysql.update('car', row, options)
      if (result) {
        const carList = await this.app.mysql.select('car')
        ctx.body = {
          code: 200,
          carList
        }
      }
    }
  }

  // 加入购物车数据
  async getCar() {
    const { ctx } = this
    const carList = await this.app.mysql.select('car')
    ctx.body = {
      code: 200,
      carList
    }
  }

  // 购物车加加
  async addCar() {
    const { ctx } = this
    const { id } = ctx.request.body
    const post = await this.app.mysql.get('car', { id });
    if (post) {
      const row = {
        id,
        num: post.num + 1
      };
      const result = await this.app.mysql.update('car', row)
      if (result.affectedRows === 1) {
        const carList = await this.app.mysql.select('car')
        console.log(carList)
        ctx.body = {
          code: 200,
          carList
        }
      }
    }
  }

  // 购物车减减
  async delCar() {
    const { ctx } = this
    const { id } = ctx.request.body
    const post = await this.app.mysql.get('car', { id });
    if (post) {
      const row = {
        id,
        num: post.num - 1
      };
      const result = await this.app.mysql.update('car', row)
      if (result.affectedRows === 1) {
        const carList = await this.app.mysql.select('car')
        ctx.body = {
          code: 200,
          carList
        }
      }
    }
  }

  // 购物车删除
  async del() {
    const { ctx } = this
    const { id } = ctx.request.body
    const res = await this.app.mysql.delete('car', { id })
    if (res.affectedRows === 1) {
      const carList = await this.app.mysql.select('car')
      console.log(carList)
      ctx.body = {
        code: 200,
        carList
      }
    }
  }


  /************* vue3+ts购物车请求  getCarList  getCarDetail getCar car addList ***************/
  // 加入购物车
  async car() {
    const { ctx } = this
    const { title, id, price, img, num } = ctx.request.body
    console.log(title, id, price, img, num)
    ctx.body = {
      code: 200
    }
    const post = await this.app.mysql.get('car', { infoId: id });
    if (post === null) {
      const result = await this.app.mysql.insert('car', {
        title,
        id,
        price,
        img,
        num: 1,
        infoId: id
      })
      if (result) {
        const carList = await this.app.mysql.select('car')
        ctx.body = {
          code: 200,
          carList
        }
      }
    } else {
      const row = {
        num: post.num + 1
      };

      const options = {
        where: {
          infoId: id
        }
      };
      const result = await this.app.mysql.update('car', row, options)
      if (result) {
        const carList = await this.app.mysql.select('car')
        ctx.body = {
          code: 200,
          carList
        }
      }
    }
  }

  // 购物车加加
  async addList() {
    const { ctx } = this
    const { id } = ctx.query
    const post = await this.app.mysql.get('car', { id });
    console.log(post)
    if (post) {
      const row = {
        id,
        num: post.num + 1
      };
      const result = await this.app.mysql.update('car', row)
      console.log(result)
      if (result.affectedRows === 1) {
        const carList = await this.app.mysql.select('car')
        console.log(carList)
        ctx.body = {
          code: 200,
          carList
        }
      }
    }
  }

  // 购物车减减
  async delList() {
    const { ctx } = this
    const { id } = ctx.query
    const post = await this.app.mysql.get('car', { id });
    console.log(post)
    if (post) {
      const row = {
        id,
        num: post.num - 1
      };
      const result = await this.app.mysql.update('car', row)
      console.log(result)
      if (result.affectedRows === 1) {
        const carList = await this.app.mysql.select('car')
        console.log(carList)
        ctx.body = {
          code: 200,
          carList
        }
      }
    }
  }

  async delCar() {
    const { ctx } = this
    const { id } = ctx.query
    const res = await this.app.mysql.delete('car', { id })
    if (res.affectedRows === 1) {
      const carList = await this.app.mysql.select('car')
      console.log(carList)
      ctx.body = {
        code: 200,
        carList
      }
    }
  }


  /******************************react增删改查登录注册********************************************** */
  // 注册
  async sigin() {
    const { ctx } = this
    const { username, password } = ctx.request.body
    const res = await this.app.mysql.get('user', { username })
    if (res) {
      ctx.body = {
        code: 500,
        msg: '注册失败'
      }
    } else {
      const list = await this.app.mysql.insert('user', { username, password, authcodes: 10003 })
      ctx.body = {
        code: 200,
        msg: '注册成功',
      }
    }
  }

  // 登录
  async login() {
    const { ctx } = this
    const { username, password } = ctx.request.body
    const list = await this.app.mysql.select('user', { username, password })
    ctx.body = {
      code: 200,
      msg: '登录成功',
      list
    }
  }

  // 获取表格数据
  async getList() {
    const { ctx } = this
    const list = await this.app.mysql.select('tablelist')
    ctx.body = {
      code: 200,
      list
    }
  }
}

module.exports = HomeController;
